package simulador;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;

import camera.CompareImage;

public class TestJFrame extends JFrame implements ActionListener {
	Timer timer = new Timer(100, this);
	JLabel lblT = new JLabel();
	BufferedImage BF = new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB);
	InfoComp IC;
	int count = 0;
	int passAct = 0; // Passo em que esta o motor de passo
	int numPass = 0; // Quantos passos ele vai dar
	public JTextField textField;
	private int cont = 0;

	public TestJFrame(InfoComp IC) {
		this.IC = IC;
		timer.start();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 369, 0, 0, 0, 0, 0, 65, 0 };
		gridBagLayout.rowHeights = new int[] { 239, 23, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 1.0,
				0.0, 0.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 0.0, Double.MIN_VALUE };
		getContentPane().setLayout(gridBagLayout);

		GridBagConstraints gbc_lblT = new GridBagConstraints();
		gbc_lblT.fill = GridBagConstraints.BOTH;
		gbc_lblT.insets = new Insets(0, 0, 5, 5);
		gbc_lblT.gridx = 0;
		gbc_lblT.gridy = 0;
		getContentPane().add(lblT, gbc_lblT);
		lblT.setIcon(new ImageIcon(BF));

		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridheight = 2;
		gbc_panel.gridwidth = 4;
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 2;
		gbc_panel.gridy = 0;
		getContentPane().add(panel, gbc_panel);

		JButton btnLiga1 = new JButton("ligaled1");
		panel.add(btnLiga1);

		JButton btnDesliga1 = new JButton("desligaled1");
		panel.add(btnDesliga1);

		JButton btnLiga2 = new JButton("Liga Led 2");
		btnLiga2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ligaled2();
			}
		});
		panel.add(btnLiga2);

		JButton btnDesliga2 = new JButton("Desliga Led 2");
		btnDesliga2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desligaled2();
			}
		});
		panel.add(btnDesliga2);

		JButton btnLiga3 = new JButton("Liga Led 3");
		btnLiga3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ligaled3();
			}
		});
		panel.add(btnLiga3);

		JButton btnDesliga3 = new JButton("Desliga Led 3");
		btnDesliga3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desligaled3();
			}
		});
		panel.add(btnDesliga3);

		JButton btnLiga4 = new JButton("Liga Led 4");
		btnLiga4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ligaled4();
			}
		});
		panel.add(btnLiga4);

		JButton btnDesliga4 = new JButton("Desliga Led 4");
		btnDesliga4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desligaled4();
			}
		});
		panel.add(btnDesliga4);

		JButton btnLigaMC2 = new JButton("Liga MC 1");
		btnLigaMC2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ligaMC2();
			}
		});
		panel.add(btnLigaMC2);

		JButton btnDesligaMC2 = new JButton("Desliga MC 2");
		btnDesligaMC2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desligaMC2();
			}
		});
		panel.add(btnDesligaMC2);

		JButton btnLigaMp = new JButton("Liga MP");
		btnLigaMp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getNumPass();
				ligaMP();
			}
		});
		panel.add(btnLigaMp);

		textField = new JTextField();
		panel.add(textField);
		textField.setColumns(10);
		btnDesliga1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				desligaled1();
			}
		});
		btnLiga1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ligaled1();
			}
		});

		atualizaTela();
	}

	public void getNumPass() {
		try {
			this.numPass = Integer.parseInt(textField.getText());
		} catch (Exception e) {
			this.numPass = 0;
		}
	}

	public InfoComp getIC() {
		return IC;
	}

	// Metodos para controle dos componentes(teste)
	private void desligaled1() {
		this.IC.setLed1(false);
	}

	private void ligaled1() {
		this.IC.setLed1(true);
	}

	private void desligaled2() {
		this.IC.setLed2(false);
	}

	private void ligaled2() {
		this.IC.setLed2(true);
	}

	private void desligaled3() {
		this.IC.setLed3(false);
	}

	private void ligaled3() {
		this.IC.setLed3(true);
	}

	private void desligaled4() {
		this.IC.setLed4(false);
	}

	private void ligaled4() {
		this.IC.setLed4(true);
	}

	private void desligaMC2() {
		this.IC.setMC2(false);
	}

	private void ligaMC2() {
		this.IC.setMC2(true);
	}

	private void ligaMP() {
		this.IC.setMP(true);
	}

	private void verifica() {

		if (this.numPass > 0) {
			this.IC.setMP(true);
		}

		// --------------------------------------------------------------------------
		// LED
		// 1--------------------------------------------------------------------
		if (IC.isLed1()) {
			led1on();
		} else {
			led1off();
		}
		// --------------------------------------------------------------------------
		// LED
		// 2--------------------------------------------------------------------
		if (IC.isLed2()) {
			led2on();
		} else {
			led2off();
		}
		// --------------------------------------------------------------------------
		// LED
		// 3--------------------------------------------------------------------
		if (IC.isLed3()) {
			led3on();
		} else {
			led3off();
		}
		// --------------------------------------------------------------------------

		// MC1
		// ----------------------------------------------------------------------
		if (IC.isMC1()) {
			mc1on();
		} else {
			mc1off();
		}
		// --------------------------------------------------------------------------

		// MC2
		// ---------------------------------------------------------------------
		if (IC.isMC2()) {
			mc2on();
		} else {
			mc2off();
		}
		// --------------------------------------------------------------------------

		// LED
		// 4--------------------------------------------------------------------
		if (IC.isLed4()) {
			led4on();
		} else {
			led4off();
		}
		// MP
		// ----------------------------------------------------------------------
		if (IC.isMP()) {
			mpon();
		} else {
			mpoff();
		}
		// --------------------------------------------------------------------------
	}

	private void atualizaTela() {
		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource3/0.png"));

			this.BF.getGraphics().drawImage(img, 0, 0, 320, 240, 0, 0, 320,
					240, null);
			// this.BF.getGraphics().drawRect(0, 0, 10, 10);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();
		} catch (IOException e1) {

			e1.printStackTrace();
		}
	}

	private void mpoff() {

		// graphics.drawImage(BF, 138, 1, 153, 16, sx1, sy1, sx2, sy2, observer)
	}

	private void mpon() {
		BufferedImage img = null;
		cont++;
		if (cont >= 6) {
			cont = 0;
			passAct++;
		}

		int pass = (this.passAct % 30) + 1;
		try {
			img = ImageIO.read(CompareImage.class.getResource("/resource3/"
					+ pass + ".png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.BF.getGraphics().drawImage(img, 238, 9, 320, 100, 238, 9, 320,
				100, null);
		lblT.setIcon(new ImageIcon(BF));
		this.repaint();

		numPass--;
		if (numPass <= 0) {
			this.IC.setMP(false);
		}
		System.out.println(numPass + "-" + pass);
	}

	private void mc2on() {

		BufferedImage img;
		try {
			if (count == 0) // Numero par
			{
				img = ImageIO.read(CompareImage.class
						.getResource("/resource2/123.png"));
				// graphics.drawIm
				this.BF.getGraphics().drawImage(img, 83, 20, 250, 170, 83, 20,
						250, 170, null);
				lblT.setIcon(new ImageIcon(BF));
				this.repaint();
				count = 1;
			} else {
				img = ImageIO.read(CompareImage.class
						.getResource("/resource2/124.png"));
				// graphics.drawIm
				this.BF.getGraphics().drawImage(img, 83, 20, 250, 170, 83, 20,
						250, 170, null);
				lblT.setIcon(new ImageIcon(BF));
				this.repaint();

				count = 0;
			}
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	private void mc2off() {

		BufferedImage img;

		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource3/0.png"));
			this.BF.getGraphics().drawImage(img, 83, 20, 250, 170, 83, 20, 250,
					170, null);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();

		} catch (IOException e) {

			e.printStackTrace();
		}
		// graphics.drawIm
	}

	private void mc1off() {

	}

	private void mc1on() {
		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource2/132.png"));
			this.BF.getGraphics().drawImage(img, 1, 21, null);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	private void led4off() {
		// 200,50,230,70,
		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource2/83.png"));
			// graphics.drawIm
			this.BF.getGraphics().drawImage(img, 188, 166, 208, 186, 188, 166,
					208, 186, null);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();

		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	private void led4on() {
		// 200,50,230,70,
		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource2/84.png"));
			// graphics.drawIm
			this.BF.getGraphics().drawImage(img, 168, 166, 208, 186, 168, 166,
					208, 186, null);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();

		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	private void led3off() {
		// 200,50,230,70,
		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource2/70.png"));
			// graphics.drawIm
			this.BF.getGraphics().drawImage(img, 0, 154, 90, 240, 0, 154, 90,
					240, null);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();

		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	private void led3on() {

		// 200,50,230,70,
		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource2/72.png"));
			// graphics.drawIm
			this.BF.getGraphics().drawImage(img, 0, 154, 90, 240, 0, 154, 90,
					240, null);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();

		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	private void led2off() {

		// 200,50,230,70,
		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource2/55.png"));
			// graphics.drawIm
			this.BF.getGraphics().drawImage(img, 297, 110, 320, 140, 297, 110,
					320, 140, null);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();

		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	private void led2on() {

		// 200,50,230,70,

		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource2/57.png"));
			// graphics.drawIm
			this.BF.getGraphics().drawImage(img, 297, 110, 320, 140, 297, 110,
					320, 140, null);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();
		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	private void led1off() {
		// 120,0,180,40,
		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource2/40.png"));
			// graphics.drawIm
			this.BF.getGraphics().drawImage(img, 120, 0, 180, 40, 120, 0, 180,
					40, null);
			lblT.setIcon(new ImageIcon(BF));
			this.repaint();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void led1on() {

		// 120,0,180,40,
		BufferedImage img;
		try {
			img = ImageIO.read(CompareImage.class
					.getResource("/resource2/41.png"));
			this.BF.getGraphics().drawImage(img, 120, 0, 160, 50, 120, 0, 160,
					50, null);

			lblT.setIcon(new ImageIcon(BF));
			this.repaint();

		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	public void buildImage() {

	}

	public static void main(String[] args) {
		InfoComp IC = new InfoComp();
		TestJFrame TJ = new TestJFrame(IC);
		TJ.setDefaultCloseOperation(EXIT_ON_CLOSE);
		TJ.setSize(640, 480);
		TJ.setVisible(true);
		TJ.pack();

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		verifica();
	}
}
